//
//  DYFileCacheManager.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/21.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYFileCacheManager.h"

@implementation DYFileCacheManager

+ (void)cacheCompletion:(void(^)(NSString *))completion
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString * cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
                               firstObject ];
        float cache = [self folderSizeAtPath:cachPath] + [self imageCache];
        NSString *cacheText;
        if (cache > 1000)
        {
            cacheText = [NSString stringWithFormat:@"%.2fG", cache / 1000.0];
        }
        else
        {
            cacheText = [NSString stringWithFormat:@"%.2fM", cache / 1.0];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            !completion ? : completion(cacheText);
        });
    });
    
}


//首先计算一下 单个文件的大小
+ (long long)fileSizeAtPath:(NSString *)filePath
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath :filePath])
    {
        return [[manager attributesOfItemAtPath:filePath error:nil]fileSize];
    }
    return 0 ;
}

//遍历文件夹获得文件夹大小
+ (float)folderSizeAtPath:(NSString *)folderPath
{
    NSFileManager * manager = [ NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath])
    {
        return 0 ;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath]objectEnumerator];
    NSString *fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject]) != nil )
    {
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize / (1000.0 * 1000.0);
}

+ (float)imageCache
{
    return [[SDImageCache sharedImageCache] getSize] / (1000.0 * 1000.0);
}

// 清理缓存
+ (void)clearCacheComption:(dispatch_block_t)completion
{
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSString * cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
            NSArray * files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
            
            for ( NSString *p in files)
            {
                NSError *error = nil ;
                NSString *path = [cachPath stringByAppendingPathComponent:p];
                
                if ([[NSFileManager defaultManager] fileExistsAtPath:path])
                {
                    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                }
            }
            dispatch_sync(dispatch_get_main_queue(), ^{
                !completion ? : completion();
            });
        });
    }];
    
    //    [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ];
    
}

@end
